/**
 * Ku6 JS Lib
 * 
 * @module  class
 * @author	zfkun<zfkun@msn.com>
 * 
 * @packages	ZF.Dom
 * @method
 * 		addClass
 * 		removeClass
 * 		hasClass
 * 		replaceClass
 * 		toggleClass
 */
ZF.add('dom-class', function(Z) {

    var D = Z.Dom,
        S = Z.String,
        CLASS_NAME = 'className',
		ADD_CLASS = 'addClass',
		REMOVE_CLASS = 'removeClass',
		HAS_CLASS = 'hasClass',
		REPLACE_CLASS = 'replaceClass',
		TOGGLE_CLASS = 'toggleClass';
	
	D[HAS_CLASS] = function(element, className){
		return S.contains(element[CLASS_NAME], className, ' ');
	};

	D[ADD_CLASS] = function(element, className){
		if(element && !D[HAS_CLASS](element, className)) element[CLASS_NAME] = S.clean(element[CLASS_NAME] + ' ' + className);
		return this;
	};

	D[REMOVE_CLASS] = function(element, className){
		if(element && D[HAS_CLASS](element, className)) element[CLASS_NAME] = element[CLASS_NAME].replace(new RegExp('(^|\\s)' + className + '(?:\\s|$)'), '$1');
		return this;
	};
	
	D[REPLACE_CLASS] = function(element, oldClassName, newClassName) {
		return D[REMOVE_CLASS](element, oldClassName)[ADD_CLASS](element, newClassName);;
	};
	
	D[TOGGLE_CLASS] = function(element, className) {
		return D[D[HAS_CLASS](element, className) ? REMOVE_CLASS : ADD_CLASS](element, className);
	};

});